import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import UserView from '../views/user/UserView.vue'
import CartView from '../views/cart/CartView.vue'
import MeatView from '../views/product/MeatView.vue'
import VegetableView from '../views/product/VegetableView.vue'
import FruitView from '../views/product/FruitView.vue'
import OthersView from '../views/product/OthersView.vue'
import SeckillView from '../views/product/SeckillView.vue'
import ProductDetailView from '../views/product/ProductDetailView.vue'
import RegisterView from '../views/auth/RegisterView.vue'
import LoginView from '../views/auth/LoginView.vue'
import FavoritesView from '../views/user/FavoritesView.vue'
import OrderView from '../views/order/OrderView.vue'
import OrderListView from '../views/order/OrderListView.vue'
import PaymentSuccessView from '../views/order/PaymentSuccessView.vue'
import Address from '../views/user/Address.vue'
import CustomerService from '../views/user/CustomerService.vue'
import { useUserStore } from '@/stores/user'
import SeckillOrdersView from '@/views/user/SeckillOrdersView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
    },
    {
      path: '/user',
      name: 'user',
      component: UserView,
      meta: { requiresAuth: true }
    },
    {
      path: '/cart',
      name: 'cart',
      component: CartView,
    },
    {
      path: '/meat',
      name: 'meat',
      component: MeatView,
    },
    {
      path: '/vegetable',
      name: 'vegetable',
      component: VegetableView,
    },
    {
      path: '/fruit',
      name: 'fruit',
      component: FruitView,
    },
    {
      path: '/others',
      name: 'others',
      component: OthersView,
    },
    {
      path: '/product/:id',
      name: 'product-detail',
      component: ProductDetailView,
    },
    {
      path: '/register',
      name: 'register',
      component: RegisterView,
    },
    {
      path: '/login',
      name: 'login',
      component: LoginView,
    },
    {
      path: '/favorites',
      name: 'favorites',
      component: FavoritesView,
    },
    {
      path: '/order',
      name: 'order',
      component: OrderView,
    },
    {
      path: '/orders',
      name: 'orders',
      component: OrderListView,
    },
    {
      path: '/payment-success/:orderNo/:totalAmount',
      name: 'payment-success',
      component: PaymentSuccessView,
    },
    {
      path: '/address',
      name: 'address',
      component: Address,
      meta: { requiresAuth: true }  //标记需要登录
    },
    {
      path: '/payment-confirm',
      name: 'payment-confirm',
      component: () => import('../views/order/PaymentConfirmView.vue')
    },
    {
      path: '/search',
      name: 'search',
      component: () => import('@/views/product/SearchView.vue')
    },
    {
      path: '/comment-center',
      name: 'CommentCenter',
      component: () => import('../views/comment/CommentCenter.vue'),
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/customer-service/:username',
      name: 'customer-service',
      component: CustomerService,
      meta: { requiresAuth: true }  // 需要登录才能访问
    },
    {
      path: '/seckill',
      name: 'seckill',
      component: SeckillView,
    },
    {
      path: '/seckill-orders',
      name: 'seckill-orders',
      component: SeckillOrdersView,
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/logistics-tracking/:orderNo',
      name: 'logistics-tracking',
      component: () => import('@/views/logistics/LogisticsTracking.vue')
    }
  ],
})

// 全局前置守卫：判断是否登录
router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth) {
    const userStore = window.__userStore
    const token = userStore?.token?.value || userStore?.token
    if (!token) {
      // 未登录，跳转到登录页
      return next('/login')
    }
  }
  next()
})

export default router
